package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.DeliveryDetailExtendMapper;
import com.aiwei.tdjk.dto.request.DeliveryDetailPagerRequest;
import com.aiwei.tdjk.entity.Delivery;
import com.aiwei.tdjk.entity.DeliveryDetail;
import com.aiwei.tdjk.entity.DeliveryDetailExample;
import com.aiwei.tdjk.service.DeliveryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeliveryDetailServiceImpl implements DeliveryDetailService{

    @Autowired
    private DeliveryDetailExtendMapper deliveryDetailExtendMapper;


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<DeliveryDetail> find(DeliveryDetailPagerRequest request) {
        request.webParamConvert();
        List<DeliveryDetail> list = deliveryDetailExtendMapper.find(request);
        int totalCount = deliveryDetailExtendMapper.getTotalCount(request);
        Pager<DeliveryDetail> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }


    /**
     * 获取
     * @param id
     * @return
     */
    @Override
    public DeliveryDetail get(Long id){
        return deliveryDetailExtendMapper.selectByPrimaryKey(id);
    }


    /**
     * 新增
     *
     * @param deliveryDetail
     * @return
     */
    @Override
    public int add(DeliveryDetail deliveryDetail) {
        return deliveryDetailExtendMapper.insertSelective(deliveryDetail);
    }

    /**
     * 更新
     *
     * @param deliveryDetail
     */
    @Override
    public void update(DeliveryDetail deliveryDetail) {
        deliveryDetailExtendMapper.updateByPrimaryKeySelective(deliveryDetail);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        deliveryDetailExtendMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据deliveryId查询发货详情列表
     */
    @Override
    public List<DeliveryDetail> getListByDeliveryId(Long deliveryId) {
        DeliveryDetailExample example = new DeliveryDetailExample();
        example.or().andDeliveryIdEqualTo(deliveryId.toString());
        return deliveryDetailExtendMapper.selectByExample(example);
    }

    @Override
    public void deleteListByDeliveryId(Long deliveryId) {
        List<DeliveryDetail> deliveryDetails = getListByDeliveryId(deliveryId);
        if (deliveryDetails!=null&&deliveryDetails.size()>0){
            for (DeliveryDetail deliveryDetail:deliveryDetails){
                delete(deliveryDetail.getId());
            }
        }
    }

    @Override
    public void deleteListByDeliveryId(List<Delivery> deliverys) {
        if (deliverys!=null&&deliverys.size()>0){
            for (Delivery delivery:deliverys){
                deleteListByDeliveryId(delivery.getId());
            }
        }
    }

}
